package com.tj.jinzao.activity;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tandong.bottomview.view.BottomView;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.callback.ISignView;
import com.tj.jinzao.common.Constant;
import com.tj.jinzao.manager.UserManager;
import com.tj.jinzao.model.Mission;
import com.tj.jinzao.model.Sign;
import com.tj.jinzao.model.TaskBean;
import com.tj.jinzao.presenter.SignPresenter;
import com.tj.jinzao.utils.ToastUtils;
import com.tj.jinzao.utils.Util;
import com.tj.jinzao.utils.Utils;
import com.tj.jinzao.widgets.CalendarView;
import com.tj.jinzao.widgets.SignDialog;
import com.tj.jinzao.widgets.SignRuleDialog;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;

public class SignActivity extends BaseActivity implements ISignView<List<Sign>> {

    @BindView(R.id.calendar_view)
    CalendarView mCalendarView;
    @BindView(R.id.tv_date)
    TextView mDateView;
    @BindView(R.id.btn_my_coin)
    Button mCoinView;
    @BindView(R.id.tv_sign_number)
    TextView mSignNumberView;
    @BindView(R.id.tv_comment)
    TextView mCommentView;
    @BindView(R.id.tv_follow)
    TextView mFollowView;
    @BindView(R.id.tv_praise)
    TextView mPraiseView;
    @BindView(R.id.tv_person)
    TextView mPersonDataView;
    @BindView(R.id.tv_weixin)
    TextView mWeixinView;
    @BindView(R.id.tv_qq)
    TextView mQQView;
    @BindView(R.id.tv_weibo)
    TextView mWeiboView;
    @BindView(R.id.txt_1)
    TextView txt_1;
    @BindView(R.id.txt_7)
    TextView txt_7;
    @BindView(R.id.txt_8)
    TextView txt_8;
    @BindView(R.id.iv_1)
    RelativeLayout iv_1;
    @BindView(R.id.iv_2)
    RelativeLayout iv_2;
    @BindView(R.id.iv_3)
    RelativeLayout iv_3;
    @BindView(R.id.iv_4)
    RelativeLayout iv_4;
    @BindView(R.id.iv_5)
    RelativeLayout iv_5;
    @BindView(R.id.iv_6)
    RelativeLayout iv_6;
    @BindView(R.id.iv_7)
    RelativeLayout iv_7;

    private SignPresenter mPresenter;
    private BottomView bottomView;
    private IWXAPI api;
    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System.currentTimeMillis();
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_sign;
    }
    private void showPopWindow(){
        bottomView=new BottomView(SignActivity.this, R.style.BottomViewTheme_Defalut, R.layout.share_item_pop);
        bottomView.setAnimation(R.style.myStyle);
        bottomView.dismissBottomView();
        bottomView.showBottomView(false);
        TextView exit=bottomView.getView().findViewById(R.id.exit);
        LinearLayout lay_1=bottomView.getView().findViewById(R.id.lay_1);
        LinearLayout lay_2=bottomView.getView().findViewById(R.id.lay_2);
        LinearLayout lay_3=bottomView.getView().findViewById(R.id.lay_3);
        LinearLayout lay_4=bottomView.getView().findViewById(R.id.lay_4);
        lay_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Util.shareWeb(SignActivity.this,api,SendMessageToWX.Req.WXSceneSession);

                bottomView.dismissBottomView();
            }
        });
        lay_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.showToast(SignActivity.this,"此功能暂未开放");
                bottomView.dismissBottomView();
            }
        });
        lay_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ToastUtils.showToast(SignActivity.this,"此功能暂未开放");
                bottomView.dismissBottomView();
            }
        });
        lay_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Util.shareWeb(SignActivity.this,api,SendMessageToWX.Req.WXSceneTimeline);
                bottomView.dismissBottomView();
            }
        });
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottomView.dismissBottomView();
            }
        });

    }
    private String mInviteCode;

    @Override
    protected void setupView() {
        super.setupView();
        api = WXAPIFactory.createWXAPI(this, Constant.WX_APPID);
        api.registerApp(Constant.WX_APPID);

        mInviteCode = UserManager.getInstance().getUser().getRequestCode();

        mPresenter = new SignPresenter(this);
        Calendar calendar = Calendar.getInstance();
        int month = (calendar.get(Calendar.MONTH) + 1);
        mDateView.setText(calendar.get(Calendar.YEAR) + "年" + ((month < 10) ? ("0" + month) : month) + "月" + calendar.get(Calendar.DAY_OF_MONTH) + "日");
        mCoinView.setText("我的早币 " + UserManager.getInstance().getUser().getScore());
        mPresenter.getSignList(true);
        mPresenter.getMission();
        findViewById(R.id.bt_invite).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopWindow();
            }
        });
        iv_4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent to=new Intent();
                to.putExtra("user",UserManager.getInstance().getUser());
                to.setClass(SignActivity.this,PersonInfoActivity.class);
                startActivity(to);
            }
        });

    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @Override
    protected CharSequence getRightButtonText() {
        return "签到规则";
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mCalendarView.onDestroy();
        if (mPresenter != null) mPresenter.detachView();
    }

    @Override
    protected void clickRightButton() {
        super.clickRightButton();
        SignRuleDialog dialog = new SignRuleDialog.Builder(this).create();
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void onRequestSuccess(List<Sign> signs) {
        mSignNumberView.setText("累计签到" + (signs == null ? 0 : signs.size()) + "天");
        List<String> date = new ArrayList();
        for (Sign sign : signs) {
            String dayStr = sign.getCheckDay();
            try {
                date.add(String.valueOf(Utils.parseInt(dayStr.split("-")[2])));
            } catch (Exception e) {
            }
        }
        mCalendarView.setSignDate(date);
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }

    @Override
    public void onSignSuccess() {
        SignDialog dialog = new SignDialog.Builder(this).create();
        dialog.setCancelable(true);
        dialog.show();
    }

    @Override
    public void onGetMission(List<Mission> missionList) {
        if(missionList==null){
            return;
        }
        Log.w("数据而技术",""+missionList.toString());
        for (int i=0;i<missionList.size();i++){
            if(missionList.get(i).getTaskCode().equals("follow")){
                if(missionList.get(i).getRemainTimes()<=0){
                    iv_2.setVisibility(View.GONE);
                }else {
                    iv_2.setVisibility(View.VISIBLE);
                    mFollowView.setText("还有"+missionList.get(i).getRemainTimes()+"次");
                }
            }else if(missionList.get(i).getTaskCode().equals("comment")){

                if(missionList.get(i).getRemainTimes()<=0){
                    iv_1.setVisibility(View.GONE);
                }else {
                    iv_1.setVisibility(View.VISIBLE);
                    mCommentView.setText("还有"+missionList.get(i).getRemainTimes()+"次");
                }
            }else if(missionList.get(i).getTaskCode().equals("like")){
                if(missionList.get(i).getRemainTimes()<=0){
                    iv_3.setVisibility(View.GONE);
                }else {
                    iv_3.setVisibility(View.VISIBLE);
                    mPraiseView.setText("还有"+missionList.get(i).getRemainTimes()+"次");
                }
            }else if(missionList.get(i).getTaskCode().equals("info")){
                if(missionList.get(i).getRemainTimes()<=0){
                    iv_4.setVisibility(View.GONE);
                }else {
                    iv_4.setVisibility(View.VISIBLE);
                    mPersonDataView.setText("去完成");
                }
            }else if(missionList.get(i).getTaskCode().equals("info")){
                if(missionList.get(i).isSuccess()){
                    iv_4.setVisibility(View.GONE);
                }else {
                    iv_4.setVisibility(View.VISIBLE);
                }
            }else if(missionList.get(i).getTaskCode().equals("weixin")){
                if(missionList.get(i).isSuccess()){
                    iv_5.setVisibility(View.GONE);
                }else {
                    iv_5.setVisibility(View.VISIBLE);
                }
            }else if(missionList.get(i).getTaskCode().equals("qq")){
                if(missionList.get(i).isSuccess()){
                    iv_6.setVisibility(View.GONE);
                }else {
                    iv_6.setVisibility(View.VISIBLE);
                }
            }else if(missionList.get(i).getTaskCode().equals("weibo")){
                if(missionList.get(i).isSuccess()){
                    iv_7.setVisibility(View.GONE);
                }else {
                    iv_7.setVisibility(View.VISIBLE);
                }
            }
        }
        if(iv_1.getVisibility()==View.GONE&&iv_2.getVisibility()==View.GONE&&iv_3.getVisibility()==View.GONE){
            txt_7.setVisibility(View.GONE);
        }else {
            txt_7.setVisibility(View.VISIBLE);
        }
        if(iv_4.getVisibility()==View.GONE&&iv_5.getVisibility()==View.GONE&&iv_6.getVisibility()==View.GONE&&iv_7.getVisibility()==View.GONE){
            txt_8.setVisibility(View.GONE);
        }else {
            txt_8.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onTaskSuccess(List<TaskBean> list) {

    }

    @Override
    public void onTaskFailed(String msg) {

    }


}
